﻿using mds.BaseFrameModel;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mds.NoSqlDatabase
{
    public class RedisProvider : IRedisProvider
    {
       public RedisProvider() {
        
       }
       public T Get<T>(string key, string host, int port)
       {
           T result;
           try
           {
               using (var client = new RedisClient(host, port))
               {
                   result = client.Get<T>(key);
               }
               if (result != null)
                   return result;
           }
           catch (Exception ex)
           {
               NosqlLogService.Instance.Log("host:" + host + ",port:" + port.ToString() + "Get失败", ex);
              
           }
           return default(T);
       }
       public OperationResult<bool> Save<K, KV>(K key, KV data, string host, int port)
       {
           OperationResult<bool> result = new OperationResult<bool>(true);
           try
           {
               using (var client = new RedisClient(host, port))
               {
                   var resultdata = client.Add<KV>(key.ToString(),data);
                   if (!resultdata)
                       result.ActionResult = false;
               }
           }
           catch (Exception ex)
           {
               result.ActionResult = false;
               NosqlLogService.Instance.Log("host:" + host + ",port:" + port.ToString() + "Save失败", ex);
           }
           return result;
       }
       public BaseFrameModel.OperationResult<bool> Save<T>(T data, string host, int port) where T : class, new()
       {
           OperationResult<bool> result = new OperationResult<bool>(true);
           try
           {
               using (var client = new RedisClient(host, port))
               {
                   var resultdata = client.Store<T>(data);
                   if (resultdata == null)
                       result.ActionResult = false;
               }
           }
           catch (Exception ex)
           {
               result.ActionResult = false;
               NosqlLogService.Instance.Log("host:" + host + ",port:" + port.ToString() + "Save失败", ex);
           }
           return result;
       }

       public BaseFrameModel.OperationResult<bool> Delete(string key, string host, int port)
       {
           OperationResult<bool> result = new OperationResult<bool>(true);
           try
           { using (var client = new RedisClient(host, port))
               {
                   var resultdata = client.Del(key);
               if (resultdata == 0)
                   result.ActionResult = false;
               }
           }
           catch (Exception ex)
           {
               result.ActionResult = false;
               NosqlLogService.Instance.Log("host:" + host +",port:"+port.ToString()+ "Delete失败", ex);
           }
           return result;
       }

       public BaseFrameModel.OperationResult<bool> Deletes(List<string> keys, string host, int port)
       {
           OperationResult<bool> result = new OperationResult<bool>(true);
           try
           {
               using (var client = new RedisClient(host, port))
               {
                   var resultdata = client.Del(keys.ToArray());
                   if (resultdata == 0)
                       result.ActionResult = false;
               }
           }
           catch (Exception ex)
           {
               result.ActionResult = false;
               NosqlLogService.Instance.Log("host:" + host + ",port:" + port.ToString() + "Deletes失败", ex);
           }
           return result;
       }

       public Dictionary<string, T> Gets<T>(List<string> keys, string host, int port)
       {
           Dictionary<string, T> result;
           try
           {
               using (var client = new RedisClient(host, port))
               {
                   result = (Dictionary<string, T>)client.GetAll<T>(keys);
                   if (result != null)
                       return result;
               }
           }
           catch (Exception ex)
           {
               NosqlLogService.Instance.Log("host:" + host + ",port:" + port.ToString() + "Gets失败", ex);
           }
           return null;
       }


       
    }
}
